﻿namespace IBTrader.Charts.Legend
{
    using IBTrader.Modules.Read;
    using NLog;
    using System.Drawing;
    using System.Globalization;
    using System.Linq;

    class LegendTime : BaseChart
    {
        private static readonly CultureInfo ci = CultureInfo.InvariantCulture;
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();
        public LegendTime(Worker worker) : base(worker) { }
        public override void Draw(IGraphics g, Charts.Region region)
        {
            if (Lines.Count <= 1) return;
            var interval = (long)region.X.Interval(Lines.Select(l => (double)l.DateTime.Ticks).ToArray());
            var lines = Lines.GroupBy(l => l.DateTime.Ticks / interval).Select(r => r.First());
            foreach (var line in lines)
            {
                var x = region.x(line);
                g.DrawString(line.DateTime.ToString("HH:mm", ci), Font, new SolidBrush(Color), new PointF(x, region.Y.Offset));
                g.DrawLines(new Pen(Color), new PointF(x, region.Y.Offset), new PointF(x, ((region.Y.Max - region.Y.Min) / region.Y.Pixel) + region.Y.Offset));
            }
        }
    }
}